import {
  network
} from "../../../utils/api"
Page({

  data: {
    house_id: '',
    work_id: '',
    html_txt: '',
    qm: false,
    addr_id:'',
    is_live_room:'',
    work_time:'',
    url: '',
    flag: true, //是否可以点击按钮
    progress: 0, //下载进度
  },

  onLoad(options) {
    if (options.work_id) {
      this.setData({
        work_id: options.work_id,
        url: network + '?i=5&from=wxapp&c=entry&a=wxapp2&m=zh_cjdianc&do=GetWordPdf&work_id=' + options.work_id
      })
      this.GetWordContents()

    } else if (options.qm) {
      this.setData({
        qm: true,
        house_id: options.id,
        addr_id:options.addr_id,
        is_live_room:options.is_live_room,
        work_time:options.worktime
      })
      this.HouseGetWordContents()
    }
  },

  GetWordContents() {
    getApp().globalData.post({
      data: {
        do: 'GetWordContents',
        a: 'wxapp2',
        work_id: this.data.work_id,
      }
    }).then(res => {
      this.setData({
        html_txt: res.data
      })
    })
  },

  GetWordPdf() {
    // 附件下载
    if (!this.data.flag) {
      return
    }
    if (this.data.progress == 100) {
      wx.openDocument({
        filePath: this.data.filePath,
        success: function (res) {
          console.log('打开文档成功')
        }
      })
      return
    }
    wx.showModal({
      content: '是否下载附件',
      success: r => {
        if (r.confirm) {
          this.setData({
            flag: false
          })
          const downloadTask = wx.downloadFile({
            url: this.data.url,
            success: (res) => {
              if (res.statusCode == 200) {
                const filePath = res.tempFilePath
                this.setData({
                  filePath: res.tempFilePath,
                  progress: 100,
                  flag: true
                })
                wx.openDocument({
                  filePath: filePath,
                  success: function (res) {
                    console.log('打开文档成功')
                  }
                })
              }
            }
          })
          downloadTask.onProgressUpdate((res) => {
            console.log(res)
            if (res.progress == 100) {
              this.setData({
                progress: res.progress,
                flag: true
              })
            }
          })
        } else if (r.cancel) {

        }
      }
    })
  },

  HouseGetWordContents() {
    getApp().globalData.post({
      data: {
        do: 'HouseGetWordContents',
        a: 'wxapp2',
        house_id: this.data.house_id,
        uid:wx.getStorageSync('userInfo').id,
        addr_id:this.data.addr_id,
        is_live_room:this.data.is_live_room,
        work_time:this.data.work_time,
      }
    }).then(res => {
      this.setData({
        html_txt: res.data
      })
    })
  },

  bottomTap() {
    wx.navigateTo({
      url: './handwriting',
    })
  },

})